﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using DAL;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Globalization;
using System.Threading;
using System.Text.RegularExpressions;

namespace BIZ
{
    public static class WebUtils
    {
        public static SinhNhatDBDataContext db = new SinhNhatDBDataContext();
        public static string GetMd5Hash(string input)
        {
            MD5 md5Hash = MD5.Create();
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public static Guid ToGuid(string str)
        {
            try
            {
                Guid id = new Guid(str.ToString());

                return id;
            }
            catch (ArgumentNullException)
            {
            }
            catch (FormatException)
            {
            }
            catch (OverflowException)
            {
            }

            return Guid.Empty;
        }

        public static string FindExtension(string filename)
        {
            string extension = string.Empty;

            if (filename.Contains("."))
            {
                int index = filename.LastIndexOf('.');
                extension = filename.Substring(index);
            }

            return extension;
        }

        public static bool checkImageFile(string extensionFile)
        {
            if (extensionFile.Equals(".jpg") || extensionFile.Equals(".png") || extensionFile.Equals(".bmp") ||
                    extensionFile.Equals(".jpeg") || extensionFile.Equals(".gif"))
            {
                return true;
            }

            return false;
        }

        public static bool deleteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool FileExist(string filePath)
        {
            if (File.Exists(filePath))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool sendMail(string from, string pass, string to, string subject, string body)
        {
            bool result = false;

            try
            {
                SmtpClient host = new SmtpClient("smtp.gmail.com", 587);
                host.EnableSsl = true;
                host.DeliveryMethod = SmtpDeliveryMethod.Network;
                host.UseDefaultCredentials = false;
                host.Credentials = new NetworkCredential(from, pass);

                MailMessage mail = new MailMessage(from, to, subject, body);
                mail.IsBodyHtml = true;
                host.Send(mail);

                result = true;
            }
            catch (Exception)
            {
                return false;
            }

            return result;
        }
        public static string ConvertToUnSign(string text)
        {
            text = text.ToLower();
            for (int i = 0x20; i < 0x30; i++)
            {
                text = text.Replace(((char)i).ToString(), " ");
            }
            text = text.Replace(".", "-");
            text = text.Replace(" ", "-");
            text = text.Replace(",", "-");
            text = text.Replace(";", "-");
            text = text.Replace(":", "-");
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string input = text.Normalize(NormalizationForm.FormD);
            return regex.Replace(input, string.Empty).Replace('đ', 'd').Replace('Đ', 'D');
        }

 

        public static string CorrectURL(string url)
        {
            url = ConvertToUnSign(url);
            
            if (url[0] != '/')
            {
                url = "/" + url;
            }

            if (url.Length > 4)
            {
                string tail = url.Substring(url.Length - 4, 4);
                if (!tail.Equals("aspx"))
                {
                    url += ".aspx";
                }
            }
            else
            {
                url += ".aspx";
            }

            return url;
        }

        public static string EditURL(string url)
        {
            url = url.Remove(0, 1);
            url = url.Remove(url.Length - 5, 5);

            return url;
        }

        public static void FormatDate()
        {
            CultureInfo culutreInfo = new CultureInfo("fr-FR");
            culutreInfo.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            culutreInfo.DateTimeFormat.DateSeparator = "/";
            Thread.CurrentThread.CurrentCulture = culutreInfo;
        }
    }
}
